package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.blade.msg.DataMessage;
import org.blade.msg.ErrorCode;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.GoodsSpecificationGroup;
import org.smr.ministore.entities.GoodsSpecificationGroupOrgan;
import org.smr.ministore.entities.vo.Assign2OrganVO;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.GoodsSpecificationGroupOrganService;
import org.smr.ministore.service.GoodsSpecificationGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;


@Controller("GoodsSpecificationGroupController")
public class GoodsSpecificationGroupController {

    @Resource(name = "goodsSpecificationGroupServiceImpl")
    private GoodsSpecificationGroupService service;

    @Resource(name = "goodsSpecificationGroupOrganServiceImpl")
    private GoodsSpecificationGroupOrganService goodsSpecificationGroupOrganService;

    /**
     * 保存编辑后的GoodsSpecificationGroup信息
     *
     * @param GoodsSpecificationGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/GoodsSpecificationGroup/edit")
    public Message modify(@RequestBody GoodsSpecificationGroup GoodsSpecificationGroup) {
        Message msg = new Message();

        if (null == GoodsSpecificationGroup.getId()) {
            msg.setStatus(service.save(GoodsSpecificationGroup));
        } else {
            msg.setStatus(service.updateById(GoodsSpecificationGroup));
        }
        return msg;
    }

    /**
     * 跳转到列表查询GoodsSpecificationGroup
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/GoodsSpecificationGroup/list-page")
    public String toList() {
        return "/GoodsSpecificationGroup/GoodsSpecificationGroup-list";
    }

    /**
     * 根据id查询GoodsSpecificationGroup
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/GoodsSpecificationGroup")
    public GoodsSpecificationGroup get(Long id) {
        return service.queryById(id);
    }


    /**
     * 分页查询GoodsSpecificationGroup信息
     *
     * @param GoodsSpecificationGroup
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/list")
    public Pager<GoodsSpecificationGroup> list(@RequestBody GoodsSpecificationGroup GoodsSpecificationGroup, long pageSize, long pageNum) {

        return service.paginate(GoodsSpecificationGroup, pageSize, pageNum);

    }


    /**
     * 根据ID删除GoodsSpecificationGroup信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/remove", method = RequestMethod.DELETE)
    public Message remove(Long id) {

        Message msg = new Message();
        msg.setStatus(service.deleteById(id));
        return msg;
    }

    /**
     * 指派给组织
     *
     * @param assignee 受派人
     * @return 影响的行数
     */
    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/assign2Organ", method = RequestMethod.POST)
    public Message assign(@RequestBody Assign2OrganVO assignee) {

        List<GoodsSpecificationGroupOrgan> references = GoodsSpecificationGroup.instance().assignTo(assignee);
        int effectCode = this.goodsSpecificationGroupOrganService.batchSave(references);
        if (ErrorCode.Rpc.FUSING == effectCode) {
            return Message.fail(ErrorCode.Rpc.FUSING_EFFECT, "Fusing effect!");
        }
        return Message.success("assign success!");
    }


    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/removeByIdLogical", method = RequestMethod.DELETE)
    public Message deleteByIdLogical(Long id) {

        int effectCode = this.service.deleteByIdLogical(id);
        if (ErrorCode.Rpc.FUSING == effectCode) {
            return Message.fail(ErrorCode.Rpc.FUSING_EFFECT, "Fusing effect!");
        }

        return Message.success("Assign success!");
    }

    /**
     * 多个商品指派给组织
     *
     * @param assign2Organs 商品集合指派给组织
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/assign2Organs", method = RequestMethod.POST)
    public Message assign(@RequestBody Assign2OrgansVO assign2Organs) {

        if (CollectionUtils.isEmpty(assign2Organs.getAssignees())) {
            return Message.fail(ErrorCode.Persistence.NONE_EFFECT, "None effect!");
        }
        List<GoodsSpecificationGroupOrgan> goodsSpecificationOrganPersistence =
                this.goodsSpecificationGroupOrganService.selectBy(assign2Organs);
        GoodsSpecificationGroup goodsSpecification = GoodsSpecificationGroup.instance();
        goodsSpecification.setCreateBy(assign2Organs.getCreateBy());
        goodsSpecification.setUpdateBy(assign2Organs.getUpdateBy());
        goodsSpecification.want2Assign(goodsSpecificationOrganPersistence, assign2Organs);
        List<GoodsSpecificationGroupOrgan> goodsOrgans = goodsSpecification.assignTo(assign2Organs);
        if (CollectionUtils.isEmpty(goodsOrgans)) {
            return Message.fail(ErrorCode.Persistence.NONE_EFFECT, "None effect!");
        }
        Message msg = new Message();
        msg.setStatus(this.goodsSpecificationGroupOrganService.batchSave(goodsOrgans));
        msg.setDescription("Assign success!");
        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/GoodsSpecificationGroup/references", method = RequestMethod.POST)
    public Message getReferences(@RequestBody Assign2OrgansVO assign2Organs){

        if(Objects.isNull(assign2Organs.getId())) {
            return Message.fail(ErrorCode.Fail.FAIL, "No data!");
        }

        List<GoodsSpecificationGroupOrgan> goodsSpecificationOrganPersistence =
                this.goodsSpecificationGroupOrganService.selectBy(assign2Organs);
        Message msg = new DataMessage(ErrorCode.Success.SUCCESS,"", goodsSpecificationOrganPersistence);
        return msg;
    }

}
